import os
import Config
import LogHelper
import uvicorn
import shutil
import json
from collections import OrderedDict
from fastapi import FastAPI

app = FastAPI()
logger = None  # 日志

@app.post('/news/getNews')
def getNews():
	global logger
	try:
		logger.logger.info('获取新闻')
		filename="news.json"
		jsontext=""
		with open(filename, 'r',encoding='utf-8') as file_object:
			jsontext= file_object.read()

		jsontext= json.loads(jsontext,object_pairs_hook=OrderedDict,encoding='utf-8')
		return {"code":0 ,"content":jsontext }
	except Exception as e:
		logger.logger.error('getNews Error : ' + str(e))
		return {"code":-1 ,"content":str(e) }


#服务
def http_server():
	name = "reptileApi"
	if os.path.exists(name):
	    shutil.rmtree(name)
	os.makedirs(name)
	uvicorn.run(app=app,
	            host=Config.ServerHostConfig(),
	            port=Config.ServerPortConfig(),
	            workers=1)	

def main():
	global logger
	logger = LogHelper.LogHelper("reptileApi.log")
	logger.logger.info('启动执行')
	try:
		http_server()
	except Exception as e:
		logger.logger.error('main Error : ' + str(e))

if __name__ == "__main__":
    main()



